package pl.vanta.utils.commons;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

public class FileUtils
{
	private FileUtils()
	{
	}

	public static List<File> getAllFilesRecursive(List<File> files, FileFilter... filters)
	{
		File[] fileTab = new File[files.size()];

		files.toArray(fileTab);

		return getAllFiles(fileTab, filters);
	}

	public static List<File> getAllFilesRecursive(File[] files, FileFilter... filters)
	{
		return getAllFiles(files, filters);
	}

	private static List<File> getAllFiles(File[] files, FileFilter... filters)
	{
		List<File> result = new ArrayList<File>();

		for (File file : files)
		{
			if (file.exists())
			{
				if (file.isFile())
				{
					if (filters == null || filters.length == 0)
					{
						result.add(file);
					}
					else
					{
						for (FileFilter fileFilter : filters)
						{
							if (fileFilter.accept(file))
							{
								result.add(file);

								break;
							}
						}
					}
				}
				else
				{
					result.addAll(getAllFiles(file.listFiles(), filters));
				}
			}
		}

		return result;
	}
}